home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / ui / layout / example / DiagonalLayout.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  6.5 KB  |  251 lines

  1. import java.awt.*;
  2. import java.util.Vector;
  3.  
  4. public class DiagonalLayout implements LayoutManager {
  5.  
  6.     private int vgap;
  7.     private int minWidth = 0, minHeight = 0;
  8.     private int preferredWidth = 0, preferredHeight = 0;
  9.     private boolean sizeUnknown = true;
  10.     private boolean DEBUG = false;
  11.  
  12.     public DiagonalLayout() {
  13.     this(5);
  14.     }
  15.  
  16.     public DiagonalLayout(int v) {
  17.     vgap = v;
  18.     }
  19.  
  20.     /* Required by LayoutManager. */
  21.     public void addLayoutComponent(String name, Component comp) {
  22.     }
  23.  
  24.     /* Required by LayoutManager. */
  25.     public void removeLayoutComponent(Component comp) {
  26.     }
  27.  
  28.     private void setSizes(Container parent) {
  29.     int nComps = parent.countComponents();
  30.     Dimension d = null;
  31.  
  32.     if (DEBUG) {
  33.         System.out.println("");
  34.         System.out.println("setSizes()");
  35.     }
  36.  
  37.     //Reset preferred/minimum width and height.
  38.     preferredWidth = 0;
  39.     preferredHeight = 0;
  40.     minWidth = 0;
  41.     minHeight = 0;
  42.  
  43.     for (int i = 0; i < nComps; i++) {
  44.         Component c = parent.getComponent(i);
  45.         if (c.isVisible()) {
  46.         d = c.preferredSize();
  47.  
  48.         if (i > 0) {
  49.             preferredWidth += d.width/2; 
  50.             preferredHeight += vgap;
  51.         } else {
  52.             preferredWidth = d.width;
  53.         }
  54.         preferredHeight += d.height;
  55.  
  56.         minWidth = Math.max(c.minimumSize().width, minWidth);
  57.         minHeight = preferredHeight;
  58.  
  59.         if (DEBUG) {
  60.             System.out.println("Component["+i+"] preferred size: " + 
  61.             c.preferredSize());
  62.             System.out.println("Component["+i+"] minimum size: " + 
  63.             c.minimumSize());
  64.                 System.out.print("preferred width: " + preferredWidth);
  65.                 System.out.print("; preferred height: " + preferredHeight);
  66.                 System.out.print("; minWidth: " + minWidth);
  67.                 System.out.println("; minHeight: " + minHeight);
  68.         }
  69.         }
  70.     }
  71.     }
  72.  
  73.  
  74.     /* Required by LayoutManager. */
  75.     public Dimension preferredLayoutSize(Container parent) {
  76.     Dimension dim = new Dimension(0, 0);
  77.     int nComps = parent.countComponents();
  78.  
  79.     if (DEBUG) {
  80.         System.out.println("");
  81.         System.out.println("Start of preferredLayoutSize()");
  82.     }
  83.  
  84.     setSizes(parent);
  85.  
  86.     //Always add the container's insets!
  87.     Insets insets = parent.insets();
  88.     dim.width = preferredWidth + insets.left + insets.right;
  89.     dim.height = preferredHeight + insets.top + insets.bottom;
  90.  
  91.     sizeUnknown = false;
  92.  
  93.     if (DEBUG) {
  94.         System.out.println("preferred layout size: " + dim);
  95.         System.out.println("End of preferredLayoutSize()");
  96.         System.out.println("");
  97.     }
  98.     return dim;
  99.     }
  100.  
  101.     /* Required by LayoutManager. */
  102.     public Dimension minimumLayoutSize(Container parent) {
  103.     Dimension dim = new Dimension(0, 0);
  104.     int nComps = parent.countComponents();
  105.  
  106.     if (DEBUG) {
  107.         System.out.println("");
  108.         System.out.println("Start of minimumLayoutSize()");
  109.     }
  110.  
  111.     //Always add the container's insets!
  112.     Insets insets = parent.insets();
  113.     dim.width = minWidth + insets.left + insets.right;
  114.     dim.height = minHeight + insets.top + insets.bottom;
  115.  
  116.     sizeUnknown = false;
  117.  
  118.     if (DEBUG) {
  119.         System.out.println("minimum layout size: " + dim);
  120.         System.out.println("End of minimumLayoutSize()");
  121.         System.out.println("");
  122.     }
  123.     return dim;
  124.     }
  125.  
  126.     /* Required by LayoutManager. */
  127.     /* This is called when the panel is first displayed, 
  128.      * and every time its size changes. 
  129.      * Note: You CAN'T assume preferredLayoutSize() or minimumLayoutSize()
  130.      * will be called -- in the case of applets, at leas, they probably
  131.      * won't be. */
  132.     public void layoutContainer(Container parent) {
  133.     Insets insets = parent.insets();
  134.     int maxWidth = parent.size().width
  135.                - (insets.left + insets.right);
  136.     int maxHeight = parent.size().height
  137.                 - (insets.top + insets.bottom);
  138.     int nComps = parent.countComponents();
  139.     int previousWidth = 0, previousHeight = 0;
  140.     int x = 0, y = insets.top;
  141.     int rowh = 0, start = 0;
  142.     int xFudge = 0, yFudge = 0;
  143.     boolean oneColumn = false;
  144.  
  145.     if (DEBUG) {
  146.         System.out.println("");
  147.         System.out.println("Start of layoutContainer()");
  148.         System.out.println("Container size = " + parent.size());
  149.     }
  150.  
  151.     // Go through the components' sizes, if neither preferredLayoutSize()
  152.     // nor minimumLayoutSize() has been called.
  153.     if (sizeUnknown) {
  154.         if (DEBUG) {
  155.         System.out.println("Calling preferredLayoutSize()");
  156.         }
  157.         setSizes(parent);
  158.     }
  159.         
  160.     if (maxWidth <= minWidth) {
  161.         oneColumn = true;
  162.     }
  163.  
  164.     if (maxWidth != preferredWidth) {
  165.         xFudge = (maxWidth - preferredWidth)/(nComps - 1);
  166.         if (DEBUG) {
  167.         System.out.println("horizontal adjustment = " + xFudge);
  168.         }
  169.     }
  170.  
  171.     if (maxHeight > preferredHeight) {
  172.         yFudge = (maxHeight - preferredHeight)/(nComps - 1);
  173.         if (DEBUG) {
  174.         System.out.println("vertical adjustment = " + yFudge);
  175.         }
  176.     }
  177.  
  178.     for (int i = 0 ; i < nComps ; i++) {
  179.         Component c = parent.getComponent(i);
  180.         if (c.isVisible()) {
  181.         Dimension d = c.preferredSize();
  182.         
  183.          // increase x and y, if appropriate
  184.         if (i > 0) { 
  185.             if (!oneColumn) {
  186.                     //x += previousWidth - d.width/2 + xFudge;
  187.             x += previousWidth/2 + xFudge;
  188.             }
  189.                 y += previousHeight + vgap + yFudge;
  190.         }
  191.         
  192.         if (DEBUG) {
  193.             System.out.println("Placing component["+i+"] at (" +
  194.             x + "," + y + ")");
  195.         }
  196.  
  197.         // If x is too large, ...
  198.         if ((!oneColumn) &&
  199.             (x + d.width) > (parent.size().width - insets.right)) {
  200.             if (DEBUG) {
  201.                 System.out.println("Eek! x too large: x="+x+"; maxX="+
  202.                 (parent.size().width - insets.right));
  203.                 System.out.println("Component["+i+"] preferred width: "
  204.                 +d.width);
  205.             }
  206.             // ... reduce x to a reasonable number.
  207.             x = parent.size().width - insets.bottom - d.width;
  208.             if (DEBUG) {
  209.                 System.out.println("Set x to " + x);
  210.             }
  211.         }
  212.  
  213.         // If y is too large, ...
  214.         if ((y + d.height) > (parent.size().height - insets.bottom)) {
  215.             if (DEBUG) {
  216.                 System.out.println("Eek! y too large: y=" + y +
  217.                 "; height=" + d.height +
  218.                 "; maxY="+ (parent.size().height - insets.bottom));
  219.                 System.out.println("Component["+i+"] preferred height: "
  220.                 +d.height);
  221.             }
  222.             // ... do nothing.
  223.             // Another choice would be to do what we do to x.
  224.         }
  225.  
  226.         // Set the component's size and position.
  227.         c.reshape(x, y, d.width, d.height);
  228.  
  229.         previousWidth = d.width;
  230.         previousHeight = d.height;
  231.         }
  232.     }
  233.  
  234.     if (DEBUG) {
  235.         System.out.println("End of layoutContainer()");
  236.         System.out.println("");
  237.     }
  238.     }
  239.     
  240.     public String toString() {
  241.     String str = "";
  242.     return getClass().getName() + "[vgap=" + vgap + str + "]";
  243.     }
  244. }
  245.  
  246. // TO DO: 
  247. // 1. Come up with a framework for making the Applet just have
  248. //    a button that pops up a window.
  249. // 2. Do event handling for Quit.
  250.  
  251.